package com.baizhi.hospital.service.impl;

import com.baizhi.hospital.dao.PatientDao;
import com.baizhi.hospital.entity.Patient;
import com.baizhi.hospital.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl implements PatientService {

	@Autowired(required = false)
	private PatientDao patientDao;

	@Override
	@Transactional(readOnly = true)
	public Map selectPatient(int page, int limit, String name) {
		if(name != null && !"".equals(name)){
			name = "%"+name+"%";
		}else{
			name = null;
		}
		int offset  = (page-1)*limit;


		List<Patient> patients = patientDao.selectPatient(offset, limit, name);
		int count = patientDao.selectCount(name);

		 Map map = new HashMap();
		 map.put("data",patients);
		 map.put("count",count);
		 map.put("code",0);
		 map.put("msg","");

		return map;
	}


	@Override
	@Transactional
	public Map insertPatient(Patient patient) {
		Map map = new HashMap();
		int i = patientDao.insertPatient(patient);
		if(i > 0){
			map.put("isSuccess","添加成功");
		}else{
			map.put("isSuccess","添加失败");
		}
		return map;
	}

	@Override
	@Transactional
	public Map updatePatient(Patient patient) {
		Map map = new HashMap();
		int i = patientDao.updatePatient(patient);
		if(i > 0){
			map.put("isSuccess","修改成功");
		}else{
			map.put("isSuccess","修改失败");
		}
		return map;
	}

	@Override
	@Transactional
	public Map deletePatient(int[] array) {
		Map map = new HashMap();
		int i = patientDao.deletePatient(array);
		if(i > 0){
			map.put("isSuccess","删除成功");
		}else{
			map.put("isSuccess","删除失败");
		}
		return map;
	}
}
